package MTT::Libs::ReportData;

use strict;

use base qw(Class::Singleton Class::Accessor);
use MTT;
use MTT::Schema;

__PACKAGE__->mk_ro_accessors( qw(schema area_data) );

use utf8;
use Data::Dumper;

sub _new_instance {
	my $class = shift;

	my $ci = MTT->config->{'Model::DBIC'}->{connect_info};
	my $schema = MTT::Schema->connect($ci);

	my $self = { schema => $schema };
	bless $self, $class;
	$self->reload();

	return $self;
}

sub get_data {
	my ($self, $report) = @_;
	
	$self->reload($report) unless $self->{data};
	return $self->{data};
}

sub get_masks {
	my ($self) = @_;

	unless ( $self->{masks} ) {
		my (@mask, @country, @area, @region, @city, @operator);

		my $rs = $self->{schema}->resultset('Mask')->search({}, {columns => 'mask'});
		$rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
		while ( my $m = $rs->next() ) {
			push @mask, $m->{mask};
		}

		$rs = $self->{schema}->resultset('Mask')->search({}, {columns => 'country', distinct => 1});
		$rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
		foreach my $m ( $rs->all() ) {
			push @country, $m->{country} if $m->{country};
		}

		$rs = $self->{schema}->resultset('Mask')->search({}, {columns => ['area'], distinct => 1});
		$rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
		foreach my $m ( $rs->all() ) {
			push @area, $m->{area} if $m->{area};
		}

		$rs = $self->{schema}->resultset('Mask')->search({}, {columns => 'region', distinct => 1});
		$rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
		foreach my $m ( $rs->all() ) {
			push @region, $m->{region} if $m->{region};
		}

		$rs = $self->{schema}->resultset('Mask')->search({}, {columns => 'city', order_by => 'city', distinct => 1});
		$rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
		foreach my $m ( $rs->all() ) {
			push @city, $m->{city} if $m->{city};
		}

		$rs = $self->{schema}->resultset('Mask')->search({}, {columns => 'operator', distinct => 1});
		$rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
		foreach my $m ( $rs->all() ) {
			push @operator, $m->{operator} if $m->{operator};
		}

		$self->{masks} = {
			mask => \@mask,
			country => \@country,
			area 	=> \@area,
			region 	=> \@region,
			city	=> \@city,
			operator => \@operator
		};
	}

	return $self->{masks};
}


sub reload {
	my ($self, $report, $schema) = @_;

	$schema ||= $self->{schema};
	die "schema need for reportStore" unless $schema;

	my $trunks =  [ map { "'$_'" } @{ $report->trunks } ] if $report && @{ $report->trunks };

	my $companies = $schema->resultset('Trunk')->search(
		{created => { '>=' => \'DATE(NOW())'}}, 
		{
			columns => [ qw(company_name) ], 
			distinct => 1, 
			order_by => ['company_name'],
		}
	);
	$companies->result_class('DBIx::Class::ResultClass::HashRefInflator');
	my @companies;
	while ( my $comp = $companies->next() ) {
		push @companies, $comp;
	}

	my $cities = $schema->resultset('Trunk')->search(
		{created => { '>=' => \'DATE(NOW())'}}, 
		{
			columns => [ qw(city) ], 
			distinct => 1, 
			order_by => ['city'],
		}
	);
	$cities->result_class('DBIx::Class::ResultClass::HashRefInflator');
	my @cities;
	while ( my $city = $cities->next() ) {
		push @cities, $city if $city->{city} =~ /[\w\d]/;
	}
	push @cities, {city => ''};

	my $obls = $schema->resultset('Trunk')->search(
		{created => { '>=' => \'DATE(NOW())'}}, 
		{
			columns => [ qw(oblast) ], 
			distinct => 1, 
			order_by => ['oblast'],
		}
	);
	$obls->result_class('DBIx::Class::ResultClass::HashRefInflator');
	my @obls;
	while ( my $obl = $obls->next() ) {
		push @obls, $obl if $obl->{oblast} =~ /[\w\d]/;
	}
	push @obls, {oblast => ''};

	my $fos = $schema->resultset('Trunk')->search(
		{created => { '>=' => \'DATE(NOW())'}}, 
		{
			columns => [ qw(fo) ], 
			distinct => 1, 
			order_by => ['fo'],
		}
	);
	$fos->result_class('DBIx::Class::ResultClass::HashRefInflator');
	my @fos;
	while ( my $fo = $fos->next() ) {
		push @fos, $fo if $fo->{fo} =~ /[\w\d]/;
	}
	push @fos, {fo => ''};

	my $trunks = $schema->resultset('Trunk')->search(
		( $trunks ? { trunks => {'IN' > $trunks}, created => { '>=' => \'DATE(NOW())' } } : {created => { '>=' => \'DATE(NOW())'} } ), 
		{
			columns => [ qw(trunk) ], 
			distinct => 1, 
			order_by => ['fo'],
		}
	);
	$trunks->result_class('DBIx::Class::ResultClass::HashRefInflator');
	my @trunks;
	while ( my $trunk = $trunks->next() ) {
		push @trunks, $trunk;
	}

	@trunks = # map { [ $_ => $_ ] } 
		sort { 
			my $ad = $a->{trunk} =~ /^\d+$/;
			my $bd = $b->{trunk} =~ /^\d+$/;
			my ($aip) = $a->{trunk} =~ /^(\d+)\./;
			my ($bip) = $b->{trunk} =~ /^(\d+)\./;

			$ad && !$bd ? -1 :
			$bd && !$ad ?  1 :
			$ad && $bd ? $a->{trunk} <=> $b->{trunk} :
			$aip && !$bip ? -1 :
			$bip && !$aip ?  1 :
			$aip && $bip ? $aip <=> $bip :
			$a->{trunk} cmp $b->{trunk}
		} @trunks;


	my @users = $schema->resultset('User')->all();
	my @switch_types = $schema->resultset('SwitchType')->search({status => 1});
	my (@params, @groups);
	foreach ( $schema->resultset('Param')->all() ) {
		if ( $_->type == 0 ) {
			push @params, $_;
		}
		else {
			push @groups, $_;
		}
	}
	
	$self->{data} = {
		params => \@params,
		groups => \@groups,
		companies => \@companies,
		cities => \@cities,
		trunks => \@trunks,
		obls => \@obls,
		fos	 => \@fos,
		users => \@users,
		switch_types => \@switch_types
	};

	$self->get_masks();
	
}


sub get_area_data {
	my ($self) = @_;

	my $rs = $self->schema
				  ->resultset('Trunk')
				  ->search({created => { '>=' => \'DATE(NOW())'}});

	$rs->result_class('DBIx::Class::ResultClass::HashRefInflator');

	my (%trunks, %companies, %cities, %regions, %areas);

	while ( my $r = $rs->next() ) {
		push @{ $trunks{ $r->{trunk} } }, {
			company => $r->{company_name},
			area	=> $r->{fo},
			region	=> $r->{oblast},
			city	=> $r->{city},
		};

		push @{ $companies{ $r->{company_name} } }, {
			trunk => $r->{trunk},
			area	=> $r->{fo},
			region	=> $r->{oblast},
			city	=> $r->{city},
		};

		push @{ $areas{ $r->{fo} } }, {
			company => $r->{company_name},
			trunk	=> $r->{trunk},
			region	=> $r->{oblast},
			city	=> $r->{city},
		};

		push @{ $regions{ $r->{oblast} } }, {
			company => $r->{company_name},
			area	=> $r->{fo},
			trunk	=> $r->{trunk},
			city	=> $r->{city},
		};

		push @{ $cities{ $r->{city} } }, {
			company => $r->{company_name},
			area	=> $r->{fo},
			region	=> $r->{oblast},
			trunk	=> $r->{trunk},
		};

	}

	$self->{area_data} = {
		trunks 	  => \%trunks,
		companies => \%companies,
		areas	  => \%areas,
		regions	  => \%regions,
		cities	  => \%cities
	};
}

sub switches_for_search {
	my ($self) = @_;

	unless ( $self->{switches_types} ) {
		my %sws;
		foreach ( $self->schema->resultset('Switch')->search({status => 1}) ) {
			$sws{ $_->host_code } = $_->id_type->type . ' ' . $_->host_code;
		}

		$self->{switches_types} = \%sws;
	}

	$self->{switches_types};
}

1;
